//
//  SleepAnalysisModel.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/3.
//

import UIKit
import HandyJSON
import MYModule

open class SleepAnalysisModel: NSObject {
    
    public var iconName: String
    public var title: String
    public var detail: String
    
    public var data: DataType
    
    open var detailAttritedText: NSMutableAttributedString {
        
        return detail.toAttributed
            .foregroundColor(UIColor.assist_A9A9A9)
            .font(Fonts.systemFont(ofSize: 12))
            .paragraph(setup: { $0.lineSpacing = 3.0 })
            .rawValue
        
    }
    
    public init(iconame: String, title: String, detail: String, data: DataType) {
        self.iconName = iconame
        self.title = title
        self.detail = detail
        self.data = data
         
        super.init()
    }
    
}

public extension SleepAnalysisModel {
    
   
    static func efficiency(number: Int) -> SleepAnalysisModel {
        
        let description = "ic_sleep_analysis_efficiency".language
        
        let model = SleepAnalysisModel(iconame: description, title: "sleep_efficiency".language, detail: "sleep_efficiency_description".language, data: DataType.efficiency(number: number))
        
        return model
        
    }
    
    static func latency(number: Int) -> SleepAnalysisModel {
        
        let model = SleepAnalysisModel(iconame: "ic_sleep_analysis_latency", title: "sleep_latency".language, detail: "sleep_latency_description".language, data: DataType.latency(number: number))
        
        return model
        
    }
    
    static func timing(type: String?) -> SleepAnalysisModel {
        
        let type = type ?? "--"
        let model = SleepAnalysisModel(iconame: "ic_sleep_analysis_timing", title: "sleep_timing".language, detail: "sleep_optimal_description".language, data: DataType.timing(type: type))
       
        return model
        
    }
    
}

public extension SleepAnalysisModel {
    
    enum DataType {
       
        case efficiency(number: Int)
        case latency(number: Int)
        case timing(type: String)
    }
    
}

public extension SleepAnalysisModel.DataType {
    
    var attritedText: NSMutableAttributedString {
        
        let textColor = UIColor.Sleep.theme
        
        switch self {
            
        case .efficiency(number: let number):
            
            let text = "\(number.normalTitle)%"
            let range = (text as NSString).range(of: "%")
            
            return text.toAttributed
                .foregroundColor(textColor)
                .font(Fonts.boldSystemFont(ofSize: 22))
                .font(Fonts.systemFont(ofSize: 10), range: range).rawValue
                
        case .latency(number: let number):
            
            let text = "\(number.normalTitle)\("unit_minute".language)"
            let range = (text as NSString).range(of: "unit_minute".language)
            
            return text.toAttributed
                .foregroundColor(textColor)
                .font(Fonts.boldSystemFont(ofSize: 22))
                .font(Fonts.systemFont(ofSize: 10), range: range).rawValue
               
        
        case .timing(type: let type):
            return type.toAttributed
                .foregroundColor(textColor)
                .font(Fonts.boldSystemFont(ofSize: 16)).rawValue
              
            
        }
        
    }
    
}
